//
//  ITTCBTableViewCell.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/31.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITTCBTableViewCell.h"


@interface ITTCBTableViewCell()

@property (nonatomic, strong) UIButton *startBtn;

@property (nonatomic, strong) UIProgressView *progressView;

@property (nonatomic, strong) ITDownloadModel *cellModel;

@end

@implementation ITTCBTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

-(void)setupViews {
    self.startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.startBtn];
    self.startBtn.frame = CGRectMake(19, 10, 80, 24);
    [self.startBtn setTitle:@"开始" forState:UIControlStateNormal];
    [self.startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(120, 20, 200, 2)];
    [self.contentView addSubview:self.progressView];
}

-(void)startAction {
    if (self.downloadBlock) {
        self.downloadBlock();
    }
}


-(void)setupModel:(ITDownloadModel *)model {
    self.cellModel = model;
    self.progressView.progress = model.progress;
    [self.cellModel addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"progress"]) {
        if (change[NSKeyValueChangeNewKey]) {
             CGFloat to = [change[NSKeyValueChangeNewKey] floatValue];
            self.progressView.progress = to;
        }
    }
}

@end
